{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "simple-examples.ipynb",
      "provenance": [],
      "collapsed_sections": [],
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/lmoroney/tfbook/blob/master/chapter9/simple-examples.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "zX4Kg8DUTKWO",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
        "# you may not use this file except in compliance with the License.\n",
        "# You may obtain a copy of the License at\n",
        "#\n",
        "# https://www.apache.org/licenses/LICENSE-2.0\n",
        "#\n",
        "# Unless required by applicable law or agreed to in writing, software\n",
        "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
        "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
        "# See the License for the specific language governing permissions and\n",
        "# limitations under the License."
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "gqWabzlJ63nL",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import numpy as np\n",
        "import matplotlib.pyplot as plt"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "sJwA96JU00pW",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "def plot_series(time, series):\n",
        "    plt.figure(figsize=(10, 6))\n",
        "    plt.plot(time, series)\n",
        "    plt.xlabel(\"time\")\n",
        "    plt.ylabel(\"value\")\n",
        "    plt.grid(True)\n",
        "    plt.show()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab_type": "code",
        "id": "t30Ts2KjiOIY",
        "colab": {}
      },
      "source": [
        "def trend(time, slope=0):\n",
        "    return slope * time"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "BLt-pLiZ0nfB",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "time = np.arange(4 * 365 + 1)\n",
        "baseline = 10\n",
        "series = trend(time, 0.1)\n",
        "plot_series(time, series)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "89gdEnPY1Niy",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "def seasonal_pattern(season_time):\n",
        "    return np.where(season_time < 0.4,\n",
        "                    np.cos(season_time * 2 * np.pi),\n",
        "                    1 / np.exp(3 * season_time))\n",
        "\n",
        "def seasonality(time, period, amplitude=1, phase=0):\n",
        "    season_time = ((time + phase) % period) / period\n",
        "    return amplitude * seasonal_pattern(season_time)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "7kaNezUk1S9l",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "baseline = 10\n",
        "amplitude = 40\n",
        "series = seasonality(time, period=365, amplitude=amplitude)\n",
        "plot_series(time, series)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "AyqFdaIN1oy5",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "slope = 0.05\n",
        "series = baseline + trend(time, slope) + seasonality(time, period=365, amplitude=amplitude)\n",
        "plot_series(time, series)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "kw0tvS8L1R_8",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "def noise(time, noise_level=1):\n",
        "    return np.random.randn(len(time)) * noise_level"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "2bRDx8K816N9",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "noise_level = 15\n",
        "noisy_series = series + noise(time, noise_level)\n",
        "plot_series(time, noisy_series)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "8tUBeSvE77Sw",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "noise_level = 40\n",
        "noisy_series = series + noise(time, noise_level)\n",
        "plot_series(time, noisy_series)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "GICxGswL2aqK",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "def autocorrelation(time, amplitude):\n",
        "    rho1 = 0.5\n",
        "    rho2 = -0.1\n",
        "    ar = np.random.randn(len(time) + 50)\n",
        "    ar[:50] = 100\n",
        "    for step in range(50, len(time) + 50):\n",
        "        ar[step] += rho1 * ar[step - 50]\n",
        "        ar[step] += rho2 * ar[step - 33]\n",
        "    return ar[50:] * amplitude"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "mCaWIWoDGVCL",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "def autocorrelation(time, amplitude):\n",
        "    rho = 0.8\n",
        "    ar = np.random.randn(len(time) + 1)\n",
        "    for step in range(1, len(time) + 1):\n",
        "        ar[step] += rho * ar[step - 1]\n",
        "    return ar[1:] * amplitude"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "MVM204K66bnC",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "series = autocorrelation(time, 10)\n",
        "plot_series(time[:200], series[:200])"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "M8C6ZBUp6jmZ",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "series = noise(time)\n",
        "plot_series(time[:200], series[:200])"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "9MZ2sCmM8XPU",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "series = autocorrelation(time, 10) + trend(time, 2)\n",
        "plot_series(time[:200], series[:200])"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "hqx5et9Bzp5e",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "series = autocorrelation(time, 10) + seasonality(time, period=50, amplitude=150) + trend(time, 2)\n",
        "plot_series(time[:200], series[:200])"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "XoRqB8AK0Sfz",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "series = autocorrelation(time, 10) + seasonality(time, period=50, amplitude=150) + trend(time, 2)\n",
        "series2 = autocorrelation(time, 5) + seasonality(time, period=50, amplitude=2) + trend(time, -1) + 550\n",
        "series[200:] = series2[200:]\n",
        "#series += noise(time, 30)\n",
        "plot_series(time[:300], series[:300])"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "5xjRZ9LC6COg",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        ""
      ],
      "execution_count": 0,
      "outputs": []
    }
  ]
}
